package com.xiaoyu.merchant.ui.activity.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.xiaoyu.commonlib.network.CommonHttpResponser;
import com.xiaoyu.commonlib.ui.base.BaseActivity;
import com.xiaoyu.commonlib.ui.widget.SendVcodeItem;
import com.xiaoyu.commonlib.utils.CheckFormatUtils;
import com.xiaoyu.commonlib.utils.StatusBarUtil;
import com.xiaoyu.commonlib.utils.ToastUtil;
import com.xiaoyu.commonlib.utils.UIUtils;
import com.xiaoyu.merchant.R;
import com.xiaoyu.merchant.network.NetworkManager;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RegisterActivity extends BaseActivity implements SendVcodeItem.ISendVcodeListener {

    @BindView(R.id.register_mobile)
    EditText mMoile;
    @BindView(R.id.register_pwd)
    EditText mPwd;
    @BindView(R.id.register_again_pwd)
    EditText mAgainPwd;
    @BindView(R.id.register_btn)
    Button mRegisterBtn;
    @BindView(R.id.activity_register_user_agreement)
    ImageView mUserAgreement;
    @BindView(R.id.register_verify_code_relative)
    RelativeLayout mVerifyCodeRelative;

    private SendVcodeItem mVerifyCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);
        StatusBarUtil.StatusBarLightMode(this);
        setOccupyViewLinearGradientLogin();
        initView();
    }

    private void initView() {
        mPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
        mAgainPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());

        mVerifyCode = new SendVcodeItem(this);
        mVerifyCode.setHint("请输入验证码");
        mVerifyCode.setSendVcodeListener(this);
        addView(mVerifyCode);

    }

    @OnClick(R.id.register_btn)
    public void registerClick() {
        if (TextUtils.isEmpty(mMoile.getText().toString()) || TextUtils.isEmpty(mPwd.getText().toString()) || TextUtils.isEmpty(mAgainPwd.getText().toString()) || TextUtils.isEmpty(mVerifyCode.getEditContent())) {
            ToastUtil.showToast(this, "请输入完整信息");
            return;
        }
        if (CheckFormatUtils.checkMobilePhone(mMoile.getText().toString()) == 2) {
            ToastUtil.showToast(this, "手机号不合法");
            return;
        }
//        if (!CheckFormatUtils.checkPassword(mPwd.getText().toString())) {
//            ToastUtil.showToast(this, "密码应由6-12位数字和字母组成");
//            return;
//        }
//        if (!CheckFormatUtils.checkPassword(mAgainPwd.getText().toString())) {
//            ToastUtil.showToast(this, "密码应由6-12位数字和字母组成");
//            return;
//        }
        if (!mAgainPwd.getText().toString().equals(mPwd.getText().toString())) {
            ToastUtil.showToast(this, "两次输入的密码不一致");
            return;
        }

        register();//调取注册接口
    }

    //调用注册接口
    private void register() {
        NetworkManager.registerUser(mMoile.getText().toString(), mVerifyCode.getEditContent(), mPwd.getText().toString(), new CommonHttpResponser() {
            @Override
            public void onSuccess(String result) {
                ToastUtil.showImgTosat(RegisterActivity.this, 1);
                finish();
            }

            @Override
            public void onFailed(String errorCode, String errorDiscription) {
                errCode(errorCode);
            }
        });
    }

    //错误码提示
    private void errCode(String errorCode) {
        if (errorCode.equals("y401")) {
            ToastUtil.showToast(RegisterActivity.this, "验证码错误");
            return;
        }
        if (errorCode.equals("y402")) {
            ToastUtil.showToast(RegisterActivity.this, "手机号已被注册");
            return;
        }
        if (errorCode.equals("y405")) {
            ToastUtil.showToast(RegisterActivity.this, "密码有误");
            return;
        }
    }

    //用户服务协议
    @OnClick(R.id.activity_register_user_agreement)
    public void userAgreementClick() {
        startActivity(new Intent(this, UserAgreementActivity.class));
    }

    @Override
    public void requestSendVcode() {
        NetworkManager.sendVerify(mMoile.getText().toString(), new CommonHttpResponser() {
            @Override
            public void onSuccess(String result) {
                ToastUtil.showToast(RegisterActivity.this, "验证码已发送到您的手机上");
            }

            @Override
            public void onFailed(String errorCode, String errorDiscription) {
            }
        });
    }


    //添加发送验证码布局
    protected void addView(View child) {
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        if (mVerifyCodeRelative.getChildCount() > 0) {
            lp.topMargin = UIUtils.dip2px(6);
        }
        mVerifyCodeRelative.addView(child, lp);
    }

    @OnClick(R.id.title_return)
    public void titleReturn() {
        finish();
    }
}
